package com.tony.vo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 
 * @author TQ
 *
 * @param <T>
 */
public class Pages<T> {
    private int total_page;     //总页数  
    private int total_count;    //总记录数  
    private int current_page;   //当前页  
    private int page_size;      //每页的数量  
    private int first_page;     //第一页页码
    private int pre_page;      //上一页页码 
    private int next_page;     //下一页页码
    private int last_page;     //最后一页页码
    private boolean is_first;  //是否为第一页
    private boolean is_last;   //是否为尾页
    private int show_page_count;  //显示分页数量
	
	private List<T> list;  
      
	
    public List<T> getList() {
		return list;
	}
	public void setList(List<T> list) {
		this.list = list;
	}
	public int getTotal_page() {  
        return total_page;  
    }  
    public void setTotal_page(int total_page) {  
        this.total_page = total_page;  
    }  
    
   
    
    
    
	public int getShow_page_count() {
		return show_page_count;
	}
	public void setShow_page_count(int show_page_count) {
		this.show_page_count = show_page_count;
	}
	public int getCurrent_page() {
		if(this.current_page>total_page){
			current_page=total_page;
		}
		return current_page;
	}
	public void setCurrent_page(int current_page) {
		this.current_page = current_page;
	}
	public int getPage_size() {
		return page_size;
	}
	public void setPage_size(int page_size) {
		this.page_size = page_size;
	}
	public int getTotal_count() {  
        return total_count;  
    }  
    public void setTotal_count(int total_count) {  
        this.total_count = total_count;  
    }  
   
    public int getFirst_page() {  
        return first_page;  
    }  
    public void setFirst_page(int first_page) {  
        this.first_page = first_page;  
    }  
    public int getPre_page() {  
        return pre_page;  
    }  
    public void setPre_page(int pre_page) {  
        this.pre_page = pre_page;  
    }  
    public int getNext_page() {  
        return next_page;  
    }  
    public void setNext_page(int next_page) {  
        this.next_page = next_page;  
    }  
    public int getLast_page() {  
        return last_page;  
    }  
    public void setLast_page(int last_page) {  
        this.last_page = last_page;  
    }  
    public boolean isIs_first() {  
        return is_first;  
    }  
    public void setIs_first(boolean is_first) {  
        this.is_first = is_first;  
    }  
    public boolean isIs_last() {  
        return is_last;  
    }  
    public void setIs_last(boolean is_last) {  
        this.is_last = is_last;  
    }  
      
    /** 
     * 分页处理 
     * @param currentPage 当前页 
     * @param pageSize 每页的数量 
     * @param total_count 总记录数 
     */  
    public void paging(int current_page, int page_size, int total_count){  
        this.current_page = current_page;  
        this.page_size = page_size;  
        this.total_count = total_count;  
        this.show_page_count = 5; 
        if(current_page < 1){  
            this.current_page = 1;  
        }  
        
        if(page_size < 1){  
            this.page_size = 20;  
        }  
          
        this.total_page = (this.total_count + this.page_size - 1)/this.page_size;  
        this.first_page =1;  
        this.last_page = total_page;  
          
        if(this.current_page > 1){  
            this.pre_page = this.current_page - 1;  
        }else{  
            this.pre_page = 1;  
        }  
          
        if(this.current_page < total_page){  
            this.next_page = this.current_page + 1;   
        }else{  
            this.next_page = total_page;  
        }  
          
        if(this.current_page <= 1){  
            this.is_first = true;  
        }else{  
            this.is_first = false;  
        }  
          
        if(this.current_page >= total_page){  
              this.is_last = true; 
        }else{  
        	  this.is_last = false;  
        }  
    }  
    
    /**
     * 分页开始页下标
     * @return
     */
    public int getStartPosition()
	{
		return (this.getCurrent_page() - 1) * this.page_size;
	}
    
//    /**
//	 * 
//	 * 
//	 * @return 返回分页HTML代码
//	 */
//	public String getNewPageHtml()
//	{
//		StringBuffer sb = new StringBuffer();
//		sb.append("共").append(this.getTotal_count()).append("条记录&nbsp;&nbsp;");
//		sb.append("当前页码:<font color=\"red\">").append(this.current_page).append("/").append(this.total_page).append("</font>&nbsp;&nbsp;");
//		sb.append("<a href=\"javascript: toPage(").append(this.first_page).append(");\">首页</a>&nbsp;");
//		sb.append("<a href=\"javascript: toPage(").append(this.pre_page).append(");\">上一页</a>&nbsp;");
//		sb.append("<input type=\"hidden\" id=\"current_page\" value=\"").append(this.current_page).append("\"/>");
//		sb.append("<input type=\"hidden\" id=\"total_page\" value=\"").append(this.total_page).append("\"/>");
//		sb.append("<a href=\"javascript: toPage(").append(this.next_page).append(");\">下一页</a>&nbsp;");
//		sb.append("<a href=\"javascript: toPage(").append(this.last_page).append(");\">尾页</a>&nbsp;");	
//		sb.append("跳到:<input type=\"text\" id=\"input_current_page\" style=\"width:40px\" value=\"").append(this.current_page).append("\" onblur=\"javascript: checkNum(this.id);\"/>页");
//		sb.append("<a id=\"current_page\" href=\"javascript: gotoPage();\">Go</a>&nbsp;");
////		&nbsp;跳到:<input type="text" name="inputCurrentPage" style="width:40px" id="inputCurrentPage" value="'+current_page+'" onblur="reg(this.id)">';
////		page_show += '页<a id="currentPage"  href="javascript:gotoPage()"> Go</a>
//		return sb.toString();
//	}
	
	
	public String getPageHtml()
	{
		StringBuffer sb = new StringBuffer();
		List<Integer> numberList = null;
		sb.append("<div class='col-xs-6'> <div class='dataTables_info'>共").append(this.getTotal_count()).append("条记录");
		sb.append("&nbsp;&nbsp;当前页码:<font color=\"red\">").append(this.current_page).append("/").append(this.total_page).append("</font></div></div>");
		sb.append("<div class='col-xs-6'><ul class='pagination pull-right no-margin'>");
		sb.append("<li class='paginate_button ");
		if(this.is_first){
			sb.append("disabled' ");
		}
		sb.append("aria-controls='dynamic-table' tabindex='0'><a href=\"javascript: toPage(").append(this.first_page).append(");\">首页</a></li>");
		sb.append("<li class='paginate_button ");
		if(this.first_page>=this.current_page){
			sb.append("disabled' ");
		}
		sb.append("aria-controls='dynamic-table' tabindex='0'><a href=\"javascript: toPage(").append(this.pre_page).append(");\">上一页</a></li>");
		sb.append("<input type=\"hidden\" id=\"current_page\" value=\"").append(this.current_page).append("\"/>");
		sb.append("<input type=\"hidden\" id=\"total_page\" value=\"").append(this.total_page).append("\"/>");
		numberList = getNumberList();
		for (int num : numberList)
		{
			if (num == this.current_page)
			{
				sb.append("<li class='paginate_button active' ");
			}
			else
			{
				sb.append("<li class='paginate_button' ");
			}
			sb.append("aria-controls='dynamic-table' tabindex='0'><a href=\"javascript: toPage(").append(num).append(");\">").append(num).append("</a></li>");
		}
		
		sb.append("<li class='paginate_button ");
		if(this.last_page<=this.current_page){
			sb.append("disabled' ");
		}
		sb.append("' aria-controls='dynamic-table' tabindex='0'><a href=\"javascript: toPage(").append(this.next_page).append(");\">下一页</a></li>");
		sb.append("<li class='paginate_button ");
		if(this.is_last){
			sb.append("disabled' ");
		}
		sb.append(" aria-controls='dynamic-table' tabindex='0'><a href=\"javascript: toPage(").append(this.last_page).append(");\">尾页</a></li>");	
		sb.append("</ul></div>");
		return sb.toString();
	}
	
	public String getPageNowHtml()
	{
		StringBuffer sb = new StringBuffer();
		List<Integer> numberList = null;
		sb.append("<div class='col-xs-5'> <div class='dataTables_info'>共").append(this.getTotal_count()).append("条记录");
		sb.append("&nbsp;&nbsp;当前页码:<font color=\"red\">").append(this.current_page).append("/").append(this.total_page).append("</font></div></div>");
		sb.append("<div class='col-xs-7'><ul class='pagination pull-right no-margin'>");
		sb.append("<li class='paginate_button ");
		if(this.is_first){
			sb.append("disabled' ");
		}
		sb.append("aria-controls='dynamic-table' tabindex='0'><a href=\"javascript: toPage_now(").append(this.first_page).append(");\">首页</a></li>");
		sb.append("<li class='paginate_button ");
		if(this.first_page>=this.current_page){
			sb.append("disabled' ");
		}
		sb.append("aria-controls='dynamic-table' tabindex='0'><a href=\"javascript: toPage_now(").append(this.pre_page).append(");\">上一页</a></li>");
		sb.append("<input type=\"hidden\" id=\"current_page_now\" value=\"").append(this.current_page).append("\"/>");
		sb.append("<input type=\"hidden\" id=\"total_page_now\" value=\"").append(this.total_page).append("\"/>");
		numberList = getNumberList();
		for (int num : numberList)
		{
			if (num == this.current_page)
			{
				sb.append("<li class='paginate_button active' ");
			}
			else
			{
				sb.append("<li class='paginate_button' ");
			}
			sb.append("aria-controls='dynamic-table' tabindex='0'><a href=\"javascript: toPage_now(").append(num).append(");\">").append(num).append("</a></li>");
		}
		
		sb.append("<li class='paginate_button ");
		if(this.last_page<=this.current_page){
			sb.append("disabled' ");
		}
		sb.append("' aria-controls='dynamic-table' tabindex='0'><a href=\"javascript: toPage_now(").append(this.next_page).append(");\">下一页</a></li>");
		sb.append("<li class='paginate_button ");
		if(this.is_last){
			sb.append("disabled' ");
		}
		sb.append(" aria-controls='dynamic-table' tabindex='0'><a href=\"javascript: toPage_now(").append(this.last_page).append(");\">尾页</a></li>");	
		sb.append("</ul></div>");
		return sb.toString();
	}
	
	
	public String getPageTwoHtml()
	{
		StringBuffer sb = new StringBuffer();
		List<Integer> numberList = null;
		sb.append("<div class='col-xs-5'> <div class='dataTables_info'>共").append(this.getTotal_count()).append("条记录");
		sb.append("&nbsp;&nbsp;当前页码:<font color=\"red\">").append(this.current_page).append("/").append(this.total_page).append("</font></div></div>");
		sb.append("<div class='col-xs-7'><ul class='pagination pull-right no-margin'>");
		sb.append("<li class='paginate_button ");
		if(this.is_first){
			sb.append("disabled' ");
		}
		sb.append("aria-controls='dynamic-table' tabindex='0'><a href=\"javascript: toPage_two(").append(this.first_page).append(");\">首页</a></li>");
		sb.append("<li class='paginate_button ");
		if(this.first_page>=this.current_page){
			sb.append("disabled' ");
		}
		sb.append("aria-controls='dynamic-table' tabindex='0'><a href=\"javascript: toPage_two(").append(this.pre_page).append(");\">上一页</a></li>");
		sb.append("<input type=\"hidden\" id=\"current_page_two\" value=\"").append(this.current_page).append("\"/>");
		sb.append("<input type=\"hidden\" id=\"total_page_two\" value=\"").append(this.total_page).append("\"/>");
		numberList = getNumberList();
		for (int num : numberList)
		{
			if (num == this.current_page)
			{
				sb.append("<li class='paginate_button active' ");
			}
			else
			{
				sb.append("<li class='paginate_button' ");
			}
			sb.append("aria-controls='dynamic-table' tabindex='0'><a href=\"javascript: toPage_two(").append(num).append(");\">").append(num).append("</a></li>");
		}
		
		sb.append("<li class='paginate_button ");
		if(this.last_page<=this.current_page){
			sb.append("disabled' ");
		}
		sb.append("' aria-controls='dynamic-table' tabindex='0'><a href=\"javascript: toPage_two(").append(this.next_page).append(");\">下一页</a></li>");
		sb.append("<li class='paginate_button ");
		if(this.is_last){
			sb.append("disabled' ");
		}
		sb.append(" aria-controls='dynamic-table' tabindex='0'><a href=\"javascript: toPage_two(").append(this.last_page).append(");\">尾页</a></li>");	
		sb.append("</ul></div>");
		return sb.toString();
	}
	
	/**
	 * 
	 * @return 返回显示分页数字数列表
	 */
	private List<Integer> getNumberList()
	{
		int middleIndex = this.show_page_count / 2 == 0 ? this.show_page_count / 2 : this.show_page_count / 2 + 1;
		List<Integer> resultList = new ArrayList<Integer>();

		if (this.total_page < this.show_page_count || this.current_page <= middleIndex)
		{
			for (int i = 1; i <= this.total_page; i++)
			{
				if (i > this.show_page_count)
				{
					break;
				}
				else
				{
					resultList.add(i);
				}
			}
		}
		else if (this.current_page > this.total_page - middleIndex)
		{
			for (int i = this.total_page - this.show_page_count + 1; i <= this.total_page; i++)
			{
				if (i < 1)
				{
					i = 1;
				}
				resultList.add(i);
			}
		}
		else
		{
			for (int i = 1; i <= this.show_page_count; i++)
			{
				resultList.add(this.current_page + middleIndex - i);
				Collections.sort(resultList);
			}
		}
		return resultList;
	}

	@SuppressWarnings("unused")
	public String getSimplePageHtml()
	{
		StringBuffer sb = new StringBuffer();
		sb.append("<a href=\"javascript: toPage(").append(this.first_page).append(");\">上一页</a>&nbsp;");
		int beforePage = this.next_page;
		int nextPage = this.current_page == 1 ? 2 : (this.current_page + 1); // 下一页
		sb.append("<a href=\"javascript: toPage(").append(nextPage).append(");\">下一页</a>&nbsp;");
		return sb.toString();
	}
}
